package com.glsc.ngateway.opmanage.service.other;

import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.opmanage.exception.PlatformException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 交易日期服务
 */
@Service
@Slf4j
public class TradingDayService {

    @Resource
    private RestTemplate restTemplate;

    @Value("${glbase.data-url}")
    private String glbaseDataUrl;

    /**
     * 查询指定日期是否是交易日
     *
     * @param currentDate
     * @return
     */
    public Boolean getToday(String currentDate) {
        ResponseEntity<PlatformResponse> forEntity = restTemplate.getForEntity("http://" + glbaseDataUrl + "/glbasedata/commonquery/getData?" +
                "infName=checkTradeDay&sysName=OPManagerSystem&pwd=glbase2opsys" +
                "&param1=" + currentDate + "&param2=SSE", PlatformResponse.class);
        if (forEntity == null || forEntity.getBody() == null || forEntity.getBody().getData() == null) {
            throw PlatformException.error("ledgerService_autoCreateJyLedger error 调用接口出错:" + forEntity);
        }
        Map data = (Map) forEntity.getBody().getData();
        List<List> context = (ArrayList<List>) data.get("context");
        if (CollectionUtils.isEmpty(context)) {
            throw PlatformException.error("ledgerService_autoCreateJyLedger error 调用接口出错:" + forEntity);
        }
        List<Boolean> contextInner = context.get(0);
        return contextInner.get(0);
    }

}
